package ar.edu.unlu.sistemas.movirdroid.business.util;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.Collection;

public class StringUtils {
	
	public static String convertToSQLSet(Collection<String> coll) {
		return convertToSQLSet(coll.toArray());
	}
	
	public static String convertToSQLSet(Object[] arr) {
		StringBuffer str = new StringBuffer();
		str.append("(");
		String temp;
		for (int i = 0; i<arr.length;i++){
			temp = (String) arr[i];
			str.append(temp+",");
		}
		str.deleteCharAt(str.length()-1);
		str.append(")");
		return str.toString();
	}
	
	public static String convertToInterrogativeSQLSet(int length) {
		StringBuffer str = new StringBuffer();
		str.append("(");
		for (int i = 0; i<length;i++) {
			str.append("?,");
		}
		str.deleteCharAt(str.length()-1);
		str.append(")");
		return str.toString();
	}
	
	public static String[] fromObjArr2StrArr(Object[] objArr) {
		String [] strArr = new String[objArr.length];
		for (int i=0;i<objArr.length;i++) {
			strArr[i] = (String)objArr[i];
		}
		return strArr;
	}
	
	public static boolean esStringVacioONull(String str) {
		return (str == null) || ("".equals(str));
	}
	
	public static String fromFloatToString(Float num) {
		return num != null ? num.toString() : "";
	}
	
	public static String parseMacAddress(String macAddr) {
		String[] macBytes = macAddr.split(":");
		StringBuffer sb = new StringBuffer();
		for (int i = 0;i<macBytes.length;i++) {
			sb.append(macBytes[i]);
		}
		return sb.toString();
	}
	
	/**
	 * Recibe una linea de un archivo de texto y retorna un array con los términos que se encontraron
	 * dentro de ésta
	 * 
	 * @param line
	 * @return Array de los términos en la línea de texto
	 * @throws UnsupportedEncodingException
	 */
	public static String[] tokenize(String line) throws UnsupportedEncodingException{
		 line = line.toLowerCase();
		 line = line.replaceAll("[óÓòÒôôöÖõÕ]", "o");
		 line = line.replaceAll("[íÍìÌîÎïÏ]", "i");
		 line = line.replaceAll("[áÁàÀâÂÄåÅãÃä]", "a");
		 line = line.replaceAll("[éÉèÈÊêëË]", "e");
		 line = line.replaceAll("[úüÚÜùÙûÛ]", "u");
		 line = line.replaceAll("ñ", "n");
		 line = line.replaceAll("\\W", " ");
		 line = line.replaceAll("[1234567890]"," ");
		 return line.split("\\s+");
	}
	
	 public static String getMIMEType(File f)
	    {
	    	String end = f.getName().substring(f.getName().lastIndexOf(".")
	    			+1, f.getName().length()).toLowerCase();
	    	String type = "";
	    	if(end.equals("mp3") || end.equals("aac") || end.equals("aac") ||
	    			end.equals("amr") || end.equals("mpeg") || end.equals("mp4")) type =
	    				"audio";
	    	else if(end.equals("jpg") || end.equals("gif") || end.equals
	    			("png") || end.equals("jpeg")) type = "image";
	    	else if(end.equals("htm") || end.equals("html") || end.equals("txt")) type = "text";

	    	type += "/*";
	    	return type;
	    }

}
